import java.util.Arrays;

import static java.lang.System.exit;

public class StackList {
    //数组实现
    int[]array;
    int size;
    int copySize;
    //初始化
    public StackList (int n){
        size=-1;
        array=new int[n];
        copySize=n;
    }
    public StackList (){
        array=new int[1];
        size=-1;
       copySize=1;
    }
    //尾插，尾删
    public void push(int p) {
        if(size==copySize-1) {
            int newCopySize=size==-1?4:2*copySize;
         array=Arrays.copyOf(array,newCopySize);
            copySize=newCopySize;
        }
        array[++size]=p;
    }
    public void pop() {
        //判断是否为空
        if (empty()) {
            System.out.println("栈为空");
            exit(0);
        }
        size--;
    }
    /** 返回栈顶元素 */
    public int top() {
        if (empty()) {
            System.out.println("栈为空");
            exit(0);
        }
        return array[size--];
    }
    /** 判断栈是否为空 */
    public boolean empty() {
        return size<0;
    }
    public void display() {
        while(!empty()) {
            System.out.print(top()+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        StackList stackList=new StackList();
        stackList.push(1);
        stackList.push(2);
        stackList.display();
        System.out.println("a");

        stackList.pop();
        System.out.println("a");
        stackList.pop();
        System.out.println("b");
        stackList.pop();

    }
}
